package exemplos.ex02.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import exemplos.ex02.modelo.FachadaUsuario;

/**
 * Servlet implementation class AutenticaSenha
 */
@WebServlet("/autentica")
public class AutenticaSenha extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AutenticaSenha() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String senha = request.getParameter("senha");
		if (senha != null && !senha.equals("")) {
			FachadaUsuario fachada = new FachadaUsuario();
			
			HttpSession session = request.getSession();
			String login = (String) session.getAttribute("login");
			
			if (fachada.autentica(login, senha)) {
				request.getRequestDispatcher("/loginRealizado").forward(request, response);
				return;
			}
			session.removeAttribute("login");
		}
		response.sendRedirect("exemplo02/erroLogin.html");
	}

}
